#include "mainwindow.h"
QStandardItemModel *model= new QStandardItemModel(1,1);
int i=-1;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    connect(&exit, SIGNAL(clicked()), this, SLOT(close()));
    connect(&dir, SIGNAL(clicked()), this, SLOT(dirr()));
    connect(&box, SIGNAL(currentIndexChanged(QString)), this, SLOT(write()));
    connect(&save, SIGNAL(clicked()), this, SLOT(resave()));
}
MainWindow::~MainWindow()
{
}
void MainWindow::dirr()
{
    Dr=QFileDialog::getExistingDirectory(this,"Directory opening");
    QDir Dir(Dr);
    QStringList list;
            list<<"*.txt";
    QStringList sl=Dir.entryList(list);
    box.addItems(sl);
}
void MainWindow::write()
{
    QFile file((Dr+char(92)+box.currentText()));
    file.open(QIODevice::ReadWrite | QIODevice::Text);
    QTextStream in(&file);
    QString txt;
    model->setRowCount(0);
    txt=in.readLine();
    i=-1;
    while (txt!="")
    {
        QStandardItem *firstRow = new QStandardItem(txt);
        i++;
        model->setItem(i,0,firstRow);
        txt=in.readLine();
    }
    file.close();
    model->setHorizontalHeaderItem(0,new QStandardItem(QString("Text of string")));
    qtab.setModel(model);
}
void MainWindow::resave()
{
    QFile file((Dr+char(92)+box.currentText()));
    file.open(QIODevice::ReadWrite | QIODevice::Text);
    QTextStream out(&file);
    for(int j=0; j<=i; j++)
    {
        QStandardItem *it=model->item(j);
        QString ab=it->text();
        out<<ab<<"\n";
    }
    file.close();
}
